<template>
  <div class="category-goods">
    <!-- 顶部导航栏 -->
    <Navbar />
    
    <div class="container">
      <!-- 面包屑导航 -->
      <el-breadcrumb separator="/" class="breadcrumb">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>分类商品</el-breadcrumb-item>
      </el-breadcrumb>

      <h2 class="page-title">分类商品</h2>

      <!-- 商品列表 -->
      <div class="goods-grid" v-loading="loading">
        <el-empty v-if="!loading && goodsList.length === 0" description="暂无商品" />
        
        <el-card 
          v-for="goods in goodsList" 
          :key="goods.goodsId"
          class="goods-card"
          :body-style="{ padding: '0px' }"
          shadow="hover"
          @click="goToDetail(goods.goodsId)"
        >
          <img :src="getImageUrl(goods.image)" class="goods-image" />
          <div class="goods-info">
            <h3 class="goods-name">{{ goods.goodsName }}</h3>
            <p class="goods-desc">{{ goods.goodsDesc }}</p>
            <div class="goods-footer">
              <span class="goods-price">¥{{ goods.goodsPrice }}</span>
              <span class="goods-stock">库存: {{ goods.sellStatus }}</span>
            </div>
            <div class="goods-extra">
              <el-tag v-if="goods.setIndex === '1'" type="danger" size="small">推荐</el-tag>
              <span class="goods-address">{{ goods.address }}</span>
            </div>
          </div>
        </el-card>
      </div>

      <!-- 分页 -->
      <el-pagination
        v-if="total > 0"
        class="pagination"
        v-model:current-page="pageNum"
        v-model:page-size="pageSize"
        :total="total"
        :page-sizes="[10, 20, 30, 50]"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { getGoodsByCategory } from '@/api/goods'
import Navbar from '@/components/Navbar.vue'
import { getFileUrl } from '@/api/file'

const route = useRoute()
const router = useRouter()

// 商品列表数据
const goodsList = ref([])
const loading = ref(false)
const pageNum = ref(1)
const pageSize = ref(10)
const total = ref(0)

// 获取商品列表
const loadGoodsByCategory = async () => {
  const categoryId = route.params.categoryId
  if (!categoryId) return

  loading.value = true
  try {
    const data = await getGoodsByCategory({
      categoryId: parseInt(categoryId),
      pageNum: pageNum.value,
      pageSize: pageSize.value
    })
    goodsList.value = data.list || []
    total.value = data.total || 0
  } catch (error) {
    console.error('获取分类商品失败:', error)
  } finally {
    loading.value = false
  }
}

// 跳转到商品详情页
const goToDetail = (goodsId) => {
  router.push(`/goods/${goodsId}`)
}

// 分页相关
const handleSizeChange = (val) => {
  pageSize.value = val
  loadGoodsByCategory()
}

const handleCurrentChange = (val) => {
  pageNum.value = val
  loadGoodsByCategory()
}

// 获取图片URL
const getImageUrl = (imagePath) => {
  return getFileUrl(imagePath)
}

// 监听路由变化
watch(() => route.params.categoryId, () => {
  pageNum.value = 1
  loadGoodsByCategory()
})

// 页面加载时
onMounted(() => {
  loadGoodsByCategory()
})
</script>

<style scoped>
.category-goods {
  width: 100%;
  min-height: 100vh;
  background-color: #f5f5f5;
  margin: 0;
  padding: 0;
}

.container {
  max-width: 1400px;
  margin: 0 auto;
  padding: 20px;
}

.breadcrumb {
  margin-bottom: 20px;
  padding-top: 20px;
}

.page-title {
  font-size: 24px;
  font-weight: bold;
  margin-bottom: 20px;
  color: #333;
}

.goods-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 20px;
  margin-bottom: 30px;
  min-height: 300px;
}

.goods-card {
  cursor: pointer;
  transition: transform 0.3s;
}

.goods-card:hover {
  transform: translateY(-5px);
}

.goods-image {
  width: 100%;
  height: 200px;
  object-fit: cover;
}

.goods-info {
  padding: 15px;
}

.goods-name {
  font-size: 16px;
  font-weight: bold;
  margin: 0 0 8px 0;
  color: #333;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.goods-desc {
  font-size: 14px;
  color: #666;
  margin: 0 0 12px 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.goods-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}

.goods-price {
  font-size: 18px;
  font-weight: bold;
  color: #ff4d4f;
}

.goods-stock {
  font-size: 12px;
  color: #999;
}

.goods-extra {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.goods-address {
  font-size: 12px;
  color: #999;
}

.pagination {
  display: flex;
  justify-content: center;
  margin-top: 30px;
}
</style>
